
package com.joolun.cloud.weixin.admin.controller;

import cn.binarywang.wx.miniapp.bean.WxMaDomainAction;
import com.joolun.cloud.common.core.util.R;
import com.joolun.cloud.weixin.admin.config.open.WxOpenConfiguration;
import com.joolun.cloud.weixin.common.constant.WxReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 代小程序实现业务
 * 基础信息设置
 *
 * @author www.joolun.com
 * @date 202-01-25 15:39:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/open/ma/settings")
@Api(value = "settings", tags = "代小程序实现业务-基础信息设置")
public class WxMaOpenSettingsController {

	/**
	 * 查询服务器域名
	 * @return
	 */
	@ApiOperation(value = "查询服务器域名")
	@GetMapping("/{appId}/serveraddress")
	@PreAuthorize("@ato.hasAuthority('wxma:serveraddress:index')")
	public R serverAddressGet(@PathVariable("appId") String appId) {
		try {
			WxMaDomainAction domainAction = new WxMaDomainAction();
			domainAction.setAction("get");
			WxMaDomainAction wxMaDomainAction = WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxFastMaServiceByAppid(appId).getSettingService().modifyDomain(domainAction);
			return R.ok(wxMaDomainAction);
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("serverAddressGet：", e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}
	}

	/**
	 * 设置服务器域名
	 * @return
	 */
	@ApiOperation(value = "设置服务器域名")
	@PostMapping("/{appId}/serveraddress")
	@PreAuthorize("@ato.hasAuthority('wxma:serveraddress:index')")
	public R serverAddressSet(@PathVariable("appId") String appId, @RequestBody WxMaDomainAction domainAction) {
		try {
			domainAction.setAction("set");
			WxMaDomainAction wxMaDomainAction = WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxFastMaServiceByAppid(appId).getSettingService().modifyDomain(domainAction);
			return R.ok(wxMaDomainAction);
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("serverAddressSet：", e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}
	}
}
